var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        dataList: '',			// 数据
		verbalTrick: '',		// 输入的文本  话术
        typedell: ''

    },
    add: function(e) {
		this.dialog.showDialog();
    },
	inputVerbalTrick: function(e) {
        this.setData({
			verbalTrick: e.detail.value
        })
    },
    // 添加话术
    addcont: function(e) {
		var message = this.data.verbalTrick;

        var that = this;
        wx.getStorage({
            key: 'user',
            success: function(res) {
                wx.request({
                    url: app.url.requestUrl + '/index/addverb',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
						content: message
                    },
                    success: function(res) {
                        if (res.data.code == 1) {
                            wx.showToast({
                                title: res.data.msg,
                            })
							that.setData({
								verbalTrick: '',
							})
							that.onLoad()
                        }
                    }
                })
            },
        })
    },
    //删除
    dell: function(e) {
        var that = this;
        var ids = e.currentTarget.dataset.id;
        var id = e.target.id;
		var dataList = this.data.dataList;

        wx.getStorage({
            key: 'user',
            success: function(res) {
                wx.request({
                    url: app.url.requestUrl + '/index/delverb',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
						id: parseInt(ids)
                    },
                    success: function(res) {
                        if (res.data.code == 1) {
                            wx.showToast({
                                title: res.data.msg,
                            })
							dataList.map((item,index) => {
								if (item.id == ids){
									dataList.splice(index,1)
								}
							})
							that.setData({
								dataList
							})
                        }
                    }
                })
            },
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
		
		this.dialog = this.selectComponent("#dialog");

        wx.getStorage({
            key: 'user',
            success: function(res) {
                wx.request({
                    url: app.url.requestUrl + '/index/verblist',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                    },
                    success: function(res) {

                        if (res.data.code == 1) {
                            that.setData({
								dataList: res.data.data,
                            })
                        }

                    }
                })
            },
        })
    },
	// 弹窗点击 取消 按钮触发函数
	__cancelEvent() {
		this.dialog.hideDialog();
	},
	// 弹窗点击 确认 按钮触发函数
	__confirmEvent() {
        var message = this.data.verbalTrick;
        if (!message){
            wx.showToast({
                image:"../../img/warning.png",
                title: '请输入话术',
            })
            return false;
        }
		this.addcont();
		this.dialog.hideDialog();
	},
})